package cn.wx.read.csv;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import com.csvreader.CsvReader;
import cn.wx.common.utils.JSONUtils;
import cn.wx.common.utils.UUIDGenerator;

public class ReadData2 {

	public static void main(String[] args) throws Exception {
		
		
		
		
		//readBook();
		
		//readPaper("126838900", "Ophthalmology","Ophthalmology\\ms_expert_paper.csv");
		//readPaper("118487528", "Radiology","Radiology\\ms_expert_paper.csv");
		
		readPatent("126838900", "Ophthalmology","Ophthalmology\\ms_expert_patent.csv");
		readPatent("118487528", "Radiology","Radiology\\ms_expert_patent.csv");

	}

	
	public static void readPatent(String fieldId,String fieldName,String dcsvName) throws IOException {
		
		StringBuffer sb = null;
		
		File file = new File("E:\\my_read_csv\\wfdata\\" + dcsvName);
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\ms_expert_patent_h.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		//"nId","title","cit","year","url","fAbstract","Authors","AuthorsId"
		//`fId`, `fTitle`, `cit`, `fPublishDate`, `url`, `fAbstract`, `fAuthors`
		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		.append(",").append(JSONUtils.c("title"))
		.append(",").append(JSONUtils.c("cit"))
		.append(",").append(JSONUtils.c("year"))
		.append(",").append(JSONUtils.c("url"))
		.append(",").append(JSONUtils.c("fAbstract"))
		.append(",").append(JSONUtils.c("Authors"))
		.append(",").append(JSONUtils.c("AuthorsId"));
		
		output.write(sb.toString());
		output.newLine();

		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取专利" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			//"nId","title","cit","year","url","fAbstract","Authors","AuthorsId"
			String nId = csvReader.get(0);
			String title = csvReader.get(2);
			String cit = csvReader.get(3);
			String year = csvReader.get(4);
			String url = csvReader.get(5);
			String fAbstract = csvReader.get(6);
			String Authors = csvReader.get(7);
			String AuthorsId = csvReader.get(8);
			

			sb = new StringBuffer();
			sb.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(title)).append(",")
			.append(JSONUtils.c(cit)).append(",")
			.append(JSONUtils.c(year)).append(",")
			.append(JSONUtils.c(url)).append(",")
			.append(JSONUtils.c(fAbstract)).append(",")
			.append(JSONUtils.c(Authors)).append(",")
			.append(JSONUtils.c(AuthorsId));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			
        }
		
        output.flush();
        output.close();
        output = null;
        
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
	}
	
	
	public static void readPaper(String fieldId,String fieldName,String dcsvName) throws IOException {
		
		StringBuffer sb = null;
		
		File file = new File("E:\\my_read_csv\\wfdata\\" + dcsvName);
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\ms_expert_paper_h.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		//"nId","title","citations","year","url","fAbstract","publisher","Authors","docType","arrayFos","AuthorsId"
		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		.append(",").append(JSONUtils.c("title"))
		.append(",").append(JSONUtils.c("citations"))
		.append(",").append(JSONUtils.c("year"))
		.append(",").append(JSONUtils.c("url"))
		.append(",").append(JSONUtils.c("fAbstract"))
		.append(",").append(JSONUtils.c("publisher"))
		.append(",").append(JSONUtils.c("Authors"))
		.append(",").append(JSONUtils.c("docType"))
		.append(",").append(JSONUtils.c("arrayFos"))
		.append(",").append(JSONUtils.c("AuthorsId"));
		
		output.write(sb.toString());
		output.newLine();

		
		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取论文" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			//"nId","title","citations","year","url","fAbstract","publisher","Authors","docType","arrayFos","AuthorsId"
			String nId = csvReader.get(0);
			String title = csvReader.get(2);
			String citations = csvReader.get(3);
			String year = csvReader.get(4);
			String url = csvReader.get(5);
			String fAbstract = csvReader.get(6);
			String publisher = csvReader.get(7);
			String Authors = csvReader.get(8);
			String docType = csvReader.get(9);
			String arrayFos = csvReader.get(10);
			String AuthorsId = csvReader.get(11);
			
			arrayFos = fieldName;
			
			sb = new StringBuffer();
			sb.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(title)).append(",")
			.append(JSONUtils.c(citations)).append(",")
			.append(JSONUtils.c(year)).append(",")
			.append(JSONUtils.c(url)).append(",")
			.append(JSONUtils.c(fAbstract)).append(",")
			.append(JSONUtils.c(publisher)).append(",")
			.append(JSONUtils.c(Authors)).append(",")
			.append(JSONUtils.c(docType)).append(",")
			.append(JSONUtils.c(arrayFos)).append(",")
			.append(JSONUtils.c(AuthorsId));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();

			
        }
		
        output.flush();
        output.close();
        output = null;
        
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
	}
	
	
	public static void readBook() throws IOException {
		
		StringBuffer sb = null;
		
		File file = new File("E:\\my_read_csv\\wfdata\\ms_expert_book.csv");
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\ms_expert_book_h.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		.append(",").append(JSONUtils.c("title"))
		.append(",").append(JSONUtils.c("Authors"))
		.append(",").append(JSONUtils.c("publisher"))
		.append(",").append(JSONUtils.c("year"))
		.append(",").append(JSONUtils.c("AuthorsId"));
		
		output.write(sb.toString());
		output.newLine();
		
		
		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			//"nId","title","publisher","year","Authors","AuthorsId"
			String nId = csvReader.get(0);
			String title = csvReader.get(2);
			String publisher = csvReader.get(3);
			String year = csvReader.get(4);
			String Authors = csvReader.get(5);
			String AuthorsId = csvReader.get(6);

			sb = new StringBuffer();
			sb.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(title)).append(",")
			.append(JSONUtils.c(Authors)).append(",")
			.append(JSONUtils.c(publisher)).append(",")
			.append(JSONUtils.c(year)).append(",")
			.append(JSONUtils.c(AuthorsId));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			
		
			
        }
		
        output.flush();
        output.close();
        output = null;
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
	}
	
	
}
